﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace uam_fps_game.Utils
{

    public class Logger
    {
        private static readonly object syncLock = new object();
        private static Logger instance;

        private static StreamWriter mStreamWriter;
        private static DateTime mDateTime;

        private const string PATH = "log.txt";
        protected Logger()
        {
            mStreamWriter = new StreamWriter(PATH, true);
        }

        public static Logger Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncLock)
                    {
                        if (instance == null)
                        {
                            instance = new Logger();
                        }
                    }
                }
                return instance;
            }
        }
        public void Log(String msg)
        {
            mDateTime = DateTime.Now;
            string time = mDateTime.ToShortTimeString() + " " + mDateTime.ToShortDateString();
            mStreamWriter.WriteLine("<<"+time+">>");
            mStreamWriter.WriteLine(msg);
            LogSave();
        }

        public void LogWarning(String msg)
        {
            mDateTime = DateTime.Now;
            string time = mDateTime.ToShortTimeString() + " " + mDateTime.ToShortDateString();
            mStreamWriter.WriteLine("<<" + time + ">>");
            mStreamWriter.WriteLine("WARNING: " + msg);
            LogSave();
        }

        public void LogError(String msg)
        {
            mDateTime = DateTime.Now;
            string time = mDateTime.ToShortTimeString() + " " + mDateTime.ToShortDateString();
            mStreamWriter.WriteLine("<<" + time + ">>");
            mStreamWriter.WriteLine("ERROR: " + msg);
            LogSave();
        }

        public void LogSave()
        {
            mStreamWriter.Close();
            mStreamWriter = new StreamWriter(PATH, true);
        }
    }
}